package edu.nf.meituan.dao.shop.impl;

import edu.nf.meituan.dao.shop.ShopAndItemAndIemTypeDAO;
import edu.nf.meituan.model.ltem.Item;
import edu.nf.meituan.model.ltem.ItemType;
import edu.nf.meituan.model.shop.Shop;
import edu.nf.meituan.model.shop.ShopAndItemAndIemType;
import edu.nf.util.DBUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

public class ShopAndItemAndIemTypeDAOImpl implements ShopAndItemAndIemTypeDAO {

    @Override
    public ShopAndItemAndIemType getShopAndItemAndIemType(Integer shopId) {
        //连接数据库对象
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        //sql执行器
        BeanHandler<Shop> shopBeanHandler = new BeanHandler(Shop.class);
        BeanListHandler<Item> itemBeanListHandler = new BeanListHandler<>(Item.class);
        BeanListHandler<ItemType> itemTypeBeanListHandler = new BeanListHandler<>(ItemType.class);
        //sql语句
        String shopSql = "SELECT items_id,shop_id, items_type_id,items_picture,items_name,items_price, item_details FROM items WHERE shop_id = ?";
        String itemSql = "SELECT items_id,shop_id, items_type_id,items_picture,items_name,items_price, item_details FROM items WHERE shop_id = ?";
        String itemTypeSql = "SELECT items_type_id, shop_id, items_type_name, items_type_at  FROM items_type  WHERE shop_id = ?";
        //结果
        Shop shop = sqlExecutor.executeQuery(shopSql, shopBeanHandler, shopId);
        List<Item> items = sqlExecutor.executeQuery(itemSql, itemBeanListHandler, shopId);
        List<ItemType> itemTypes = sqlExecutor.executeQuery(itemTypeSql, itemTypeBeanListHandler, shopId);

        return new ShopAndItemAndIemType(shop,items,itemTypes);
    }
}
